package io.noties.markwon.plugin.syntax

import android.text.SpannableStringBuilder
import android.text.Spanned
import io.noties.markwon.base.core.spans.StrongEmphasisSpan
import io.noties.markwon.base.core.spans.EmphasisSpan
import android.text.style.BackgroundColorSpan
import androidx.annotation.ColorInt


class Prism4jThemeObsidian(@param:ColorInt private val background: Int) : Prism4jThemeBase() {
    override val colorHashMap = ColorHashMap()
        .add(0xff66747B.toInt(), "comment", "prolog", "doctype", "cdata")
        .add(0xffF1F2F3.toInt(), "punctuation")
        .add(0xff8AC763.toInt(), "property", "tag", "boolean", "number", "constant", "symbol", "deleted")
        .add(0xffE0E2E4.toInt(), "selector", "attr-name", "string", "char", "builtin", "inserted")
        .add(-0x6591c6, "operator", "entity", "url")
        .add(0xffEC7600.toInt(), "atrule", "attr-value", "keyword")
        .add(-0x22b598, "function", "class-name")
        .add(-0x116700, "regex", "important", "variable")

    override fun background(): Int {
        return background
    }

    override fun textColor(): Int {
        return 0xffF1F2F3.toInt()
    }

    override fun applyColor(
        language: String,
        type: String,
        alias: String?,
        @ColorInt color: Int,
        builder: SpannableStringBuilder,
        start: Int,
        end: Int
    ) {
        var color = color
        if ("css" == language && isOfType("string", type, alias)) {
            super.applyColor(language, type, alias, -0x6591c6, builder, start, end)
            builder.setSpan(
                BackgroundColorSpan(-0x7f000001),
                start,
                end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            return
        }
        if (isOfType("namespace", type, alias)) {
            color = applyAlpha(.7f, color)
        }
        super.applyColor(language, type, alias, color, builder, start, end)
        if (isOfType("important", type, alias)
            || isOfType("bold", type, alias)
        ) {
            builder.setSpan(StrongEmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        if (isOfType("italic", type, alias)) {
            builder.setSpan(EmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
    }

    companion object {
        fun create(): Prism4jThemeObsidian {
            return Prism4jThemeObsidian(0xff000000.toInt())
        }

        /**
         * @since 3.0.0
         */
        fun create(@ColorInt background: Int): Prism4jThemeObsidian {
            return Prism4jThemeObsidian(background)
        }
    }
}